package com.feib.soeasy.dao.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.feib.soeasy.dao.WriteOffPaymentDao;
import com.feib.soeasy.model.Payment;
import com.feib.soeasy.model.UploadFile;
import com.feib.soeasy.model.User;
import com.feib.soeasy.model.WriteOffPayment;
import com.feib.soeasy.util.UserUtil;

/**
 * @title (#)WriteOffPaymentHibernateDao.java<br>
 * @description <br>
 * @author Leo Lee<br>
 * @version 1.0.0 2010/11/28
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/28 create by Lee Leo<br>
 */
@Repository("writeOffPaymentDao")
public class WriteOffPaymentHibernateDao extends BaseHibernateDao<WriteOffPayment, Long> implements WriteOffPaymentDao {

    @Override
    public WriteOffPayment save(WriteOffPayment writeOffPayment) {
        getSession().saveOrUpdate(writeOffPayment);
        return writeOffPayment;
    }
    
    public List<WriteOffPayment> getStatisticsForGroup(
    		Long groupPk, String channel, Integer qryDateType, Date qryDateFrom, Date qryDateTo)
    {
    	/*
Criteria criteria = getSession().createCriteria(Payment.class);
    	criteria.add(Restrictions.eq("payerId", payerId));
    	UploadFile uploadFile = new UploadFile();
    	uploadFile.setUploadFilePk(uploadFilePk);
    	criteria.add(Restrictions.eq("uploadFile", uploadFile));
    	criteria.addOrder(Order.asc("virtualAccount"));
    	
        return criteria.list();
    	 */
    	
    	Criteria criteria = getSession().createCriteria(WriteOffPayment.class);
    	criteria.add(Restrictions.eq("", groupPk));
    	
    	return null;
    }
    
    public void updateConfirm(ArrayList<Payment> payment, Integer status){
    	StringBuffer hql = new StringBuffer();
    	hql.append("update WriteOffPayment set writeOffStatus = :writeOffStatus  ");
    	hql.append(",confirmUser = :confirmUser  ");
    	hql.append(",confirmDate = :sysdate  ");
        hql.append("where payment in (:payment) ");
        
        User user = UserUtil.getUser();
        
    	Query query = getSession().createQuery(hql.toString());
    	query.setParameter("writeOffStatus", status);
        query.setParameter("confirmUser", user );
        query.setParameter("sysdate", new Date() );
        query.setParameterList("payment", payment);
        query.executeUpdate();
    }
    
    //2011.12 by Leo
    public WriteOffPayment getWriteOffPaymentByPayment(Payment payment)
    {
    	Criteria criteria = getSession().createCriteria(WriteOffPayment.class);
    	criteria.add(Restrictions.eq("payment", payment));
    	List list = criteria.list();
    
    	if(list != null && list.size() > 0)
    		return (WriteOffPayment)list.get(0);
    	else
    		return null;
    }
}
